<?php

class ProductsManager extends BaseManager implements iManager {

	public function query() {
		$results = array();

		$condition = $this->getCondition();

		$query = "SELECT * FROM `product_tbl`";

		if (!empty($condition)) {
			$query .= $condition;
		}

		$this->db->query($query);

		$this->db->execute();

		while ($result = $this->db->getResultAssoc()) {
			$product = new Product_Domain();
				
			$product->setProductId($result['product_id']);
			$product->setProductName($result['product_name']);
			$product->setProductQty($result['current_count']);
			$product->setProductPrice($result['current_price']);
			$product->setImgDirectory($result['product_img_dir']);
				
			$results[] = $product;
		}

		$this->result = $results;
	}
	
	public function getAvailableImages(){
		$results = array();

		//read directory
		foreach(glob('resources/images/products/*.*') as $filename){
			$results[] = $filename;
		}
		
		$this->result = $results;
	}
	
	public function getAlertProd(){
		$results = array();
		
		$query = "SELECT * FROM product_tbl WHERE current_count <= 3;";
		
		$this->db->query($query);
		
		$this->db->execute();
		
		while ($result = $this->db->getResultAssoc()) {
			$product = new Product_Domain();
		
			$product->setProductId($result['product_id']);
			$product->setProductName($result['product_name']);
			$product->setProductQty($result['current_count']);
		
			$results[] = $product;
		}
		
		$this->result = $results;
	}

	public function add() {
		// add employee
		$data['product_name'] = $this->obj->getProductName();
		$data['product_description'] = $this->obj->getProductDescription();
		$data['product_category_id'] = $this->obj->getCategory();
		$data['current_price'] = $this->obj->getProductPrice();
		$data['current_count'] = $this->obj->getProductQty();
		$data['product_img_dir'] = $this->obj->getImgDirectory();
		$data['archive'] = 'n';
		
		$this->db->insert('product_tbl', $data);
		$this->db->execute();
		
		$this->result = $this->db->getAffectedRows();
	}

	public function update() {
		
		$where['product_id'] = $this->obj->getProductId();
		
		$data['product_name'] = $this->obj->getProductName();
		$data['product_description'] = $this->obj->getProductDescription();
		$data['product_category_id'] = $this->obj->getCategory();
		$data['current_price'] = $this->obj->getProductPrice();
		$data['archive'] = $this->obj->getArchive();
		$data['product_img_dir'] = $this->obj->getImgDirectory();
		
		$this->db->update('product_tbl', $data, $where);
		
		$this->db->execute();
		
		$this->result = $this->db->getAffectedRows();
	}
	
	public function unarchive(){
		
		$where['product_id'] = $this->obj->getProductId();
		
		$data['archive'] = 'n';
		$data['archive_date'] = date('Y-m-d H:i:s');
		
		$this->db->update('product_tbl', $data, $where);
		
		$this->db->execute();
		
		$this->result = $this->db->getAffectedRows();
	}

	public function delete() {
	}
}